function voteStaticsInit() {

	var voteId = $('#voteId').val();
	if (voteId == null) {
		alert('voteId为空');
		return;
	}

	initStatics(voteId);

	initDetail(voteId);

}

function addInvite(){
	var voteId = $('#voteId').val();
	var param = {};
	param.voteId = voteId;
	if ($('#oaAccount').prop('checked')) {
		param.oaType = 0;
	} else if ($('#oaERP').prop('checked')) {
		param.oaType = 1;
	} else {
		alert('请选择OA待办类型!');
		return;
	}
	//邀请人员
	param.inviteVoter = $('#OA').val();
	
	getContent("/vote/inviteUser", param, true, function(data) {
		if (data != null) {
			alert('邀请成功!');
		}
	});
}

function downloadExcel() {
	var voteId = $('#voteId').val();
	window.location.href = "/vote/getUserDetailExcel?voteId=" + voteId;
	return false
}

function showUpdateVote(voteId,voteUserId){
	var param = {};
	param.voteId = voteId;
	param.voteUserId = voteUserId;
	showContent('/view/pokemon-updateVote.html',param,initUpdateVote);
}

function initStatics(voteId) {
	var param = {};
	param.voteId = voteId;
	postContent("/vote/getUserVoteStatics", param, true, function(data) {
		$('#totalTile').html('投票<' + data.voteTitle + '>' + '总体统计');
		$('#voteTotalNum').html(data.voteTotalNum);
		$('#voteNum').html(data.voteNum);
		$('#optionNum').html(data.optionNum);
		$('#votePercent').html(divPercent(data.voteTotalNum, data.voteNum));
		var optionStatics = data.optionStatics;
		var $tbody = $('#static').find('tbody');
		var barData = [];
		for (num in optionStatics) {
			//表格数据
			var row = "<tr>";
			row += "<td>" + optionStatics[num].showName + "</td>";
			row += "<td>" + optionStatics[num].staticsNum + "</td>";
			row += "<td>" + optionStatics[num].memo + "</td>";
			row += "</tr>";
			$tbody.append(row);
			//柱状图数据
			barData[num] = {};
			barData[num].name = optionStatics[num].showName;
			barData[num].num = optionStatics[num].staticsNum;
		}

		Morris.Bar({
			element : 'hero-bar',
			data : barData,
			xkey : 'name',
			ykeys : [ 'num' ],
			labels : [ '投票数' ],
			barRatio : 0.4,
			xLabelMargin : 10,
			hideHover : 'auto',
			barColors : [ "#3d88ba" ]
		});
	});
}

function initDetail(voteId) {
	var param = {};
	param.voteId = voteId;
	postContent(
			'/vote/getUserDetailTitle',
			param,
			true,
			function(data) {
				var $thead = $('#detail').find('thead').find('tr');
				var rows = [];
				for (num in data) {
					var head = '<th>' + data[num] + '</th>';
					$thead.append(head);
					rows[num] = {};
					rows[num].mData = 'data' + num;
				}
				//增加一个操作列
				$thead.append('<th>操作</th>');
				rows[data.length] = {};
				rows[data.length].mData = 'operation';
				$('#detail')
						.DataTable(
								getCommonPageParam(
										'/vote/getUserDetail',
										param,
										false,
										rows,
										function(resp) {
											var userList = resp.aaData;
											for (num in userList) {
												var data = userList[num].data;
												var voteUserId = userList[num].voteUserId;
												//userList[num]['operation'] = '<a href="/vote/showUpate?voteId='+voteId+'&voteUserId='+voteUserId+'>修改</a>';
												var opera = '<a class="btn-flat primary" href="#" onclick="showUpdateVote('
														+ voteId
														+ ','
														+ voteUserId
														+ ')"'
														+ '>修改</a>';
												opera += '<a class="btn-flat primary" href="#" onclick=deleteUser('
														+ voteId
														+ ','
														+ voteUserId
														+ ')>删除</a>';
												userList[num]['operation'] = opera;
												for (var i = 0; i < data.length; i++) {
													userList[num]['data' + i] = data[i];
												}
											}
											return resp;
										}));
			});
}

function deleteUser(voteId, voteUserId) {
	if (voteId == null || voteUserId == null) {
		alert('voteId voteUserId 为空');
	}
	if (confirm('确定删除该用户吗?')) {
		var param = {};
		param.voteId = voteId;
		param.voteUserId = voteUserId;
		postContent("/vote/deleteVoteUser", param, true, function(data) {
			if (data != null) {
				alert('删除成功!');
				window.location.reload();
			}
		});
	}
}



//相除百分比
function divPercent(data1, data2) {
	var f1 = parseFloat(data1);
	var f2 = parseFloat(data2);
	if (isNaN(f1) || isNaN(f2) || f2 == 0) {
		return 0;
	}

	var f = f1 / f2;
	f = Math.round(f * 10000) / 100;
	return f.toString() + "%";
}